<?php
	require_once('init.php');
	
	//Get Current Page
	if (! isset($_GET['page']) || $_GET['page'] == '') {$page = '1';} 
	else {$page = $_GET['page'];}
	
	//Isset Delete Action
	if (isset($_GET['f']) && $_GET['f'] == 'del') 
	{
		//Get Product Info
		$product = $adminDB->executeSQL("select * from ".$util->contentdbName().".tbProduct where id='".$_GET['id']."'", $connID);
		//Get Content Info
		$content = $adminDB->executeSQL("select * from ".$util->contentdbName().".tbContent where id='".$product[0]['fdContentID']."'", $connID);
		
		//Try Delete Product
		if (!$adminDB->executeSQL("update ".$util->contentdbName().".tbProduct set fdStatus = 3 where id='".$_GET['id']."'", $connID)) 
		{
			echo "<script>alert('产品信息删除失败！');</script>";
		}
		else
		{
			//Insert AdminLog
			if(!$adminDB->executeSQL("insert into ".$util->userdbName().".tbAdminLog (fdOperatorID,fdAction,fdTarget,fdTime) values('".$_SESSION['adminid']."', 'DELETE', '".$util->contentdbName().".tbProduct set fdStatus = 3 where id = ".$_GET['id']."' , now())", $connID))			
			{echo "<script>alert('产品信息删除成功！管理日志添加失败');</script>";}
			else{			
				// //Delete Content
				// $adminDB->executeSQL("delete from ".$util->contentdbName().".tbContent where id ='".$product[0]['fdContentID']."'", $connID);
				// //Delete Index
				// $adminDB->executeSQL("delete from ".$util->contentdbName().".tbIndex where fdContentID ='".$product[0]['fdContentID']."'", $connID);
				// //Delete File
				// $adminDB->executeSQL("delete from ".$util->contentdbName().".tbFile where fdContentID ='".$product[0]['fdContentID']."'", $connID);
				//Delete Relate
				$adminDB->executeSQL("delete from ".$util->contentdbName().".tbRelate where fdProductID ='".$_GET['id']."'", $connID);
				//Delete Related
				$adminDB->executeSQL("delete from ".$util->contentdbName().".tbIndex where fdRelateID ='".$_GET['id']."'", $connID);
				
				echo "<script>alert('产品信息删除成功！');</script>";
			}
		}
	}
	
	//Isset Reset Action
	if (isset($_GET['f']) && $_GET['f'] == 'reset') 
	{
		//Get Product Info
		$product = $adminDB->executeSQL("select * from ".$util->contentdbName().".tbProduct where id='".$_GET['id']."'", $connID);		
		
		//Try Delete Product
		if (!$adminDB->executeSQL("update ".$util->contentdbName().".tbProduct set fdStatus = 0 where id='".$_GET['id']."'", $connID)) 
		{
			echo "<script>alert('产品信息恢复失败！');</script>";
		}
		else
		{
			//Insert AdminLog
			if(!$adminDB->executeSQL("insert into ".$util->userdbName().".tbAdminLog (fdOperatorID,fdAction,fdTarget,fdTime) values('".$_SESSION['adminid']."', 'UPDATE', '".$util->contentdbName().".tbProduct set fdStatus = 0 where id = ".$_GET['id']."' , now())", $connID))			
			{echo "<script>alert('产品信息恢复成功！管理日志添加失败');</script>";}
			else{				
				echo "<script>alert('产品信息恢复成功！');</script>";
			}
		}
	}
	
	//Set Shown Form Default New
	$isShow = 'N';
	
	//Isset Edit Action
	if (isset($_GET['f']) && $_GET['f'] == 'edit'|| (isset($_POST['f']) && $_POST['f'] == 'edit')) 
	{
		//Confirm Post
		if (isset($_POST['f']) && $_POST['f'] == 'edit') 
		{
			if (! $adminDB->executeSQL("select fdName from ".$util->contentdbName().".tbProduct where fdName ='".trim($_POST['name'])."' and id !='".trim($_POST['id'])."'", $connID)) 
			{
				//Query Max ContnetID
				$maxContentID = $adminDB->executeSQL("select fdContentID from ".$util->contentdbName().".tbProduct where id ='".trim($_POST['id'])."'", $connID);
				
				//Try Insert Conent
				if(!$adminDB->executeSQL("update ".$util->contentdbName().".tbContent set fdName = '".trim($_POST['name'])."', fdModify = now(), fdOperatorID = '".$_SESSION['adminid']."'  where id = '".$maxContentID[0]['fdContentID']."'", $connID))
				{
					echo "<script>alert('产品Content更新失败！');</script>";
				}
				else
				{
					$savedcolumn = $adminDB->executeSQL("select fdColumnID from ".$util->contentdbName().".tbIndex where fdContentID ='".$maxContentID[0]['fdContentID']."'", $connID);
					if($_POST['column']!=$savedcolumn[0]['fdColumnID'])
					{
						//Try Update Index
						$adminDB->executeSQL("update ".$util->contentdbName().".tbIndex set fdColumnID = '".trim($_POST['column'])."' where fdContentID = '".$maxContentID[0]['fdContentID']."'", $connID);
					}
					
					
					//Try Insert Product
					if (!$adminDB->executeSQL("update ".$util->contentdbName().".tbProduct set fdName='".trim($_POST['name'])."',fdTarget='".trim($_POST['target'])."', fdListPrice='".trim($_POST['listprice'])."', fdPrice='".trim($_POST['price'])."', fdAvailabe='".trim($_POST['availabe'])."', fdExpire='".trim($_POST['expire'])."', fdDescription='".trim($_POST['description'])."', fdMemo='".trim($_POST['memo'])."', fdLicense='".trim($_POST['license'])."', fdVersion='".trim($_POST['version'])."' where id='".$_POST['id']."'", $connID)) 
					{			
						echo "<script>alert('产品信息更改失败！');</script>";			
					} 
					else {
						//Insert AdminLog
						$adminDB->executeSQL("insert into ".$util->userdbName().".tbAdminLog (fdOperatorID,fdAction,fdTarget,fdTime) values('".$_SESSION['adminid']."', 'UPDATE', '".$util->contentdbName().".tbProduct set fdName=".trim($_POST['name']).", fdListPrice=".trim($_POST['listprice']).", fdPrice=".trim($_POST['price']).", fdAvailabe=".trim($_POST['availabe']).", fdExpire=".trim($_POST['expire'])." where id=".$_POST['id']."' , now())", $connID);
						echo "<script>alert('产品信息更改成功！');</script>";
					}
				}
			} 
			else 
			{
				echo "<script>alert('已存在同名产品！请保证产品名不重复');</script>";
			}			
			
		}
		
		//Get Current ColumnID
		if (isset($_GET['id'])) 
		{
			$id = $_GET['id'];
		}
		else 
		{
			$id = $_POST['id'];
		}
		$columns = $adminDB->executeSQL("select id,fdName from ".$util->contentdbName().".tbColumn order by fdOrder", $connID);
		for ($i = 0; $i < count($columns); $i ++) {
			$arrayColumnOption[$columns[$i]['id']] = $columns[$i]['fdName'];
		}
		$smarty->assign('arrayColumnOption', $arrayColumnOption);
		
		//Query Current Column
		$product = $adminDB->executeSQL("select p.id,p.fdName,p.fdTarget,p.fdListPrice,p.fdPrice,p.fdAvailabe,p.fdExpire,p.fdMemo,p.fdDescription,p.fdLicense,p.fdVersion,m.id as fdColumnID, m.fdName as fdColumnName from ".$util->contentdbName().".tbProduct p left outer join ".$util->contentdbName().".tbContent t on t.id = p.fdContentID left outer join ".$util->contentdbName().".tbIndex i on i.fdContentID = p.fdContentID left outer join ".$util->contentdbName().".tbColumn m on m.id = i.fdColumnID where p.id = '".$id."'", $connID);
		//Assign Current Column
		$smarty->assign('product', $product);
		//Set Shown Form Edit
		$isShow = 'E';
	}
    
	//Query Product List	
	$sql = "select p.id,p.fdName,p.fdListPrice,p.fdPrice,p.fdAvailabe,p.fdExpire,p.fdContentID,m.id as fdColumnID, m.fdName as fdColumnName from ".$util->contentdbName().".tbProduct p left outer join ".$util->contentdbName().".tbContent t on t.id = p.fdContentID left outer join ".$util->contentdbName().".tbIndex i on i.fdContentID = p.fdContentID left outer join ".$util->contentdbName().".tbColumn m on m.id = i.fdColumnID order by p.id";
	$products = $pageDB->pageData($sql, $connID, 20, $page);		
	$smarty->assign('products', $products);		
	$smarty->assign('isShow', $isShow);
	$smarty->display('admin_listproduct.html');
	
	$connDB->closeConnID();
	
?>